package com.fancy.learncenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.GalleryPicDetailActivity;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.utils.LogUtil;
import com.superservice.lya.R;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hyy on 2018/1/18.
 * describe as
 */

public class GraphicAdapter extends CommonRecycleViewAdapter<String> {
    public GraphicAdapter(Context mContext, ArrayList<String> itemDatas) {
        super(mContext, R.layout.activity_releasegraphic_item, itemDatas);
    }


//    public List<EditText> editTexts = new ArrayList<>();

    public List<String> editStr = new ArrayList<>();

    public Map<Integer, String> editText = new HashMap();

    @Override
    public void bindView(final CustomViewHold customViewHold, final String item, final int position) {
        FrameLayout image_temp = customViewHold.getView(R.id.image_temp);
        image_temp.setVisibility(View.VISIBLE);


        SimpleDraweeView simpleDraweeView = customViewHold.getView(R.id.simpleDraweeView);
        simpleDraweeView.setImageURI(Uri.fromFile(new File(item)));

//        if (TextUtils.isEmpty(item)) {
//            image_temp.setVisibility(View.GONE);
//        } else {
//            simpleDraweeView.setImageURI(Uri.fromFile(new File(item)));
//        }

        EditText et_content = customViewHold.getView(R.id.et_content);
        et_content.setTag(position);

        LogUtil.MyLog("et_content", position + "======et_content===" + et_content.getTag());
        if ((Integer) (et_content.getTag()) == position) {
//            editTexts.add(et_content);
        }


        et_content.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // 输入前的监听

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听

//                if ((Integer) (customViewHold.getView(R.id.et_content).getTag()) == position) {
//                    editText.put(position, s.toString());
//
//
//                }
                editStr.set(position,s.toString());

            }
        });


        et_content.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    focusPosition = position;
                } else {
                    // 此处为失去焦点时的处理内容
                }
            }
        });

        if (picCallBack != null) {
            simpleDraweeView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    if (picCallBack != null) {
                        picCallBack.picCallBack(focusPosition, position);
                    }

                    return false;
                }
            });
        }

        simpleDraweeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList itemDatas = new ArrayList();
                itemDatas.add("file://" + item);
                Intent intent = new Intent(mContext, GalleryPicDetailActivity.class);
                intent.putExtra(GalleryPicDetailActivity.PIC_KEY, itemDatas);
                mContext.startActivity(intent);
            }
        });

    }

    public int focusPosition = 0;

    private SetPicCallBack picCallBack;

    public void setPicCallBack(SetPicCallBack picCallBack) {
        this.picCallBack = picCallBack;
    }

    public interface SetPicCallBack {
        abstract void picCallBack(int focusPosition, int position);
    }
}
